/*
 * Created on 2004-04-15
 */
package org.opencube.oms;

import java.util.Comparator;



/**
 * @author <a href="mailto:maciek@fingo.pl">FINGO - Maciej Mroczko</a>
 * TODO: comment
 */
class NodeComparator implements Comparator
{

	/* (non-Javadoc)
	 * @see com.sun.msv.datatype.xsd.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare( Object arg0, Object arg1 )
	{
		if( arg0 instanceof Relation && arg1 instanceof Relation )
		{
			return compareRelations( (Relation) arg0, (Relation) arg1 );
		}
		if( arg0 instanceof Relation )
		{
			return 1;
		}
		if( arg1 instanceof Relation )
		{
			return -1;
		}
		return 1;
	}
	private int compareRelations( Relation r1, Relation r2 )
	{
		if( r1.isComposition() && r2.isComposition() )
		{
			return r1.getTargetElement().getLevel() - r2.getTargetElement().getLevel();
		}
		if( r1.isComposition() )
		{
			return 1;
		}
		if( r2.isComposition() )
		{
			return -1;
		}
		return -1;
	}
}
